package com.app.socketclient;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 10086) {
                ToastUtil.toastShortContent(MainActivity.this, "服务端断开连接了---");
                PackageManager packageManager = getPackageManager();
                Intent intent = packageManager.getLaunchIntentForPackage("com.app.socketserver");
                if (intent == null) {
                    ToastUtil.toastShortContent(MainActivity.this, "未安装服务端---");
                } else {
                    Intent intent2 = new Intent();
                    intent2.setComponent(new ComponentName("com.app.socketserver", "com.app.socketserver.MyService"));
                    startService(intent2);
                }
            } else if (msg.what == 10001) {
                ToastUtil.toastShortContent(MainActivity.this, "服务端运行中-----");
            }
        }
    };
    private TextView textView;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_tt);
        button = findViewById(R.id.btn_tt);
        SocketClientThread clientThread = new SocketClientThread(handler);
        clientThread.start();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.toastShortContent(MainActivity.this, "22222222");
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.toastShortContent(MainActivity.this, "555555");
            }
        });

    }

}
